package com.pubinfo.passbook.api.service.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.excel.EasyExcel;
import com.pubinfo.passbook.api.service.CompanyExcelService;
import com.pubinfo.passbook.common.mapper.TRabbitmqOrgConvertMapper;
import com.pubinfo.passbook.common.mapper.TRabbitmqPersonMapper;
import com.pubinfo.passbook.common.mapper.TSystemRoleMapper;
import com.pubinfo.passbook.common.model.bo.contract.ContractExcel;
import com.pubinfo.passbook.common.model.bo.contract.ContractImportStatus;
import com.pubinfo.passbook.common.model.bo.contract.ContractStatus;
import com.pubinfo.passbook.common.model.ruler.CompanyExcel;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import com.pubinfo.passbook.common.utils.CompanyExcelDataListener;
import com.pubinfo.passbook.common.utils.ContractExcelDataListener;
import com.pubinfo.passbook.common.utils.ContractImportStatusListener;
import com.pubinfo.passbook.common.utils.ContractStatusListener;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@Service
public class CompanyExcelServiceImpl implements CompanyExcelService {
    
    @Value("${file.uploadFolder}")
    private String filePath;
    @Autowired
    TRabbitmqPersonMapper tRabbitmqPersonMapper;
    @Autowired
    TSystemRoleMapper tSystemRoleMapper;
    @Autowired
    TRabbitmqOrgConvertMapper tRabbitmqOrgConvertMapper;
    
    @Autowired
    private TContractBaseService baseService;

    @Override
    @Transactional
    public Boolean importInformation(MultipartFile file) throws IOException {
        
        EasyExcel.read(file.getInputStream(), CompanyExcel.class, new CompanyExcelDataListener(tRabbitmqPersonMapper, tSystemRoleMapper, tRabbitmqOrgConvertMapper)).headRowNumber(2).sheet().doRead();
        
        return true;
    }
    
    @SneakyThrows
    @Override
    public Boolean importContractPerson(MultipartFile multiFile) {
  
      // 获取文件名
      String fileName = multiFile.getOriginalFilename();
      // 获取文件后缀
      String prefix = fileName.substring(fileName.lastIndexOf("."));
      File excelFile = File.createTempFile(fileName, prefix);
      multiFile.transferTo(excelFile);
      EasyExcel.read(excelFile, ContractExcel.class, new ContractExcelDataListener(tRabbitmqPersonMapper, tSystemRoleMapper, tRabbitmqOrgConvertMapper, baseService)).headRowNumber(2).sheet().doRead();
  
      //保存完后删除Excel
      FileUtils.deleteQuietly(excelFile);
  
      return true;
    }
  
  @SneakyThrows
  @Override
  public Boolean importContractLocalStatus(HttpServletResponse response, String acct, MultipartFile multiFile) {
    
    // 获取文件名
    String fileName = multiFile.getOriginalFilename();
    
    // 获取文件后缀
    String prefix = fileName.substring(fileName.lastIndexOf("."));
    File excelFile = File.createTempFile(PinyinUtil.getPinyin(fileName, ""), prefix);
    multiFile.transferTo(excelFile);
    EasyExcel.read(excelFile, ContractImportStatus.class, new ContractImportStatusListener(response, acct, baseService, tRabbitmqPersonMapper)).headRowNumber(1).sheet().doRead();
    //保存完后删除Excel
    FileUtils.deleteQuietly(excelFile);
    return null;
  }
  
  @SneakyThrows
  @Override
  public Boolean importContractStatus(String acct, MultipartFile multiFile) {
    
    // 获取文件名
    String fileName = multiFile.getOriginalFilename();
    
    // 获取文件后缀
    String prefix = fileName.substring(fileName.lastIndexOf("."));
    File excelFile = File.createTempFile(PinyinUtil.getPinyin(fileName, ""), prefix);
    multiFile.transferTo(excelFile);
    EasyExcel.read(excelFile, ContractStatus.class, new ContractStatusListener(acct, baseService)).headRowNumber(1).sheet().doRead();
    
    FileUtils.deleteQuietly(excelFile);
    
    return true;
  }
}
